$package('upms.application');
upms.application = function(){
	var _box = null;
	var _this = {
		addAction:baseUrl+'/application/add',
		editAction:baseUrl+'/application/edit',
		deleteAction:baseUrl+'/application/delete',
		editForm:function(){
			return $("#editForm");
		},
		editWin:function(){
			return $("#edit-win");
		},
		add:function(){
			if(_this.editForm().form('validate')){
				_this.editForm().attr('action',_this.addAction);
				upms.saveForm(_this.editForm(),function(data){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.editWin().dialog('close');
				});
			}
		},
		edit:function(){
			if(_this.editForm().form('validate')){
				_this.editForm().attr('action',_this.editAction);
				upms.saveForm(_this.editForm(),function(data){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.editWin().dialog('close');
				});
			}
		},
	    deleteData:function(){
			var rows = $('#data-list').datagrid('getChecked');
			if (upms.checkSelect(rows)){
					$.messager.confirm('Confirm','是否删除选中记录?',function(r){  
				    if (r){
				    	var arr = [],idKey = 'applicationId'; //主键名称
				    	$.each(rows,function(i,record){
				    		arr.push('applicationIds='+record[idKey]);
				    	});
				    	var data = arr.join("&");
				    	upms.deleteForm(_this.deleteAction,data,function(){
							var param = $("#searchForm").serializeObject();
							$("#data-list").datagrid('reload',param);
						});
				    }  
				});
			}
		},
		initForm:function(){
			//新增操作
			$("#addBtn").click(function(){
				$("#editForm").resetForm();
				$("input[name=applicationKey]").removeAttr("readonly");
				$("#edit-win").dialog({
					buttons:[
						{
							text:'保  存',
							iconCls: 'icon-ok',
							handler:function(){
								_this.add();
							}
						},
						{
							text:'关  闭',
							iconCls: 'icon-cancel',
							handler:function(){
								_this.editWin().dialog('close');
							}
						}
				]});
				_this.editWin().dialog('open');
			});
			//删除操作
			$("#deleteBtn").click(function(){
				_this.deleteData();
			});
			
			//修改操作
			$("#editBtn").click(function(){
				$("#editForm").resetForm();
				//只能对一条数据编辑
				var rows = $('#data-list').datagrid('getChecked');
				if(rows.length > 1){
					upms.alert('警告','请单选记录后，再进行操作.','warning');
					return false;
				}
				var row = $('#data-list').datagrid('getSelected');
				if (row){
					upms.progress();
					var data ={};
					var idKey = 'applicationId'; //主键名称
 					data[idKey] = row.applicationId;
					upms.getById(baseUrl+"/application/getById",data,function(result){
						upms.closeProgress();
						$("#editForm").form('load',result);
						$("input[name=applicationKey]").attr("readonly","readonly");
						$("#edit-win").dialog({
	   						buttons:[
	   							{
	   								text:'保  存',
	   								iconCls: 'icon-ok',
	   								handler:function(){
	   									_this.edit();
	   								}
	   							},
	   							{
	   								text:'关  闭',
	   								iconCls: 'icon-cancel',
	   								handler:function(){
	   									_this.editWin().dialog('close');
	   								}
	   							}
	   					]});
						_this.editWin().dialog('open');
	   				});
				}else{
					upms.alert('警告','未选中记录.','warning');
				}
			});
		},
		config:{
  			dataGrid:{
	   			url:'dataList',
	   			loadMsg:"处理中，请稍候…",
			    toolbar:"#toolbar",
			    idField:"applicationId",
	   			columns:[[
						{field:'applicationId',checkbox:true},
						{field:'applicationName',title:'应用系统名称',width:160,align:'center',sortable:true},
						{field:'applicationKey',title:'应用系统KEY',width:120,align:'center',sortable:true},
						{field:'applicationUrl',title:'应用系统URL',width:260,align:'left',sortable:true},
						{field:'createTime',title:'创建时间',width:160,align:'center',sortable:true,formatter:upms.dateTimeFormatter},
						{field:'updateTime',title:'修改时间',width:160,align:'center',sortable:true,formatter:upms.dateTimeFormatter}
				]]
			}
		},
		init:function(){
			_this.initForm();
			_box = new MyDataGrid(_this.config); 
			_box.init();
		}
	}
	_this._box = _box;
	return _this;
}();

$(function(){
	upms.application.init();
});		